﻿package mortal.game.net.command.pack
{
    import Framework.MQ.*;
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.debug.*;
    import com.gengine.global.*;
    import extend.language.*;
    import flash.geom.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.msgTip.*;
    import mortal.game.mvc.*;
    import mortal.game.net.broadCast.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.copy.defenseCopy.*;
    import mortal.game.view.copy.defenseCopy.view.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.lotteryDraw.*;
    import mortal.game.view.systemSetting.*;
    import mortal.mvc.core.*;

    public class PackItemCommand extends BroadCastCall
    {
        private var _gardenFlyArr:Array;
        private var _frameT:FrameTimer;

        public function PackItemCommand(param1:Object)
        {
            this._gardenFlyArr = [];
            super(param1);
            return;
        }// end function

        override public function call(param1:MessageBlock) : void
        {
            var objChangeIndexs:Object;
            var splayerItemUpdate:SClientPlayerItemUpdate;
            var item:Object;
            var aryChangeIndexBag:Array;
            var aryChangeIndexFashion:Array;
            var packPosTypeCatch:PackPosTypeCache;
            var aryPackItems:Array;
            var itemData:ItemData;
            var fromPackCache:PackPosTypeCache;
            var aryFromPackItems:Array;
            var rmItemData:ItemData;
            var guideData:GuideUseItemData;
            var tips:String;
            var obj:Object;
            var equipType:int;
            var updateInfo:Object;
            var hType:int;
            var mb:* = param1;
            var getArrayItems:* = function (param1:int) : Array
            {
                if (!objChangeIndexs[param1])
                {
                    objChangeIndexs[param1] = [];
                }
                return objChangeIndexs[param1];
            }// end function
            ;
            var updataItems:* = mb.messageBase as SSeqClientPlayerItemUpdate;
            objChangeIndexs = new Object();
            var itemChaneNum:Object;
            var _loc_3:int = 0;
            var _loc_4:* = updataItems.playerItemUpdates;
            while (_loc_4 in _loc_3)
            {
                
                splayerItemUpdate = _loc_4[_loc_3];
                packPosTypeCatch = Cache.instance.pack.getPackChacheByPosType(splayerItemUpdate.playerItem.posType);
                aryPackItems = this.getArrayItems(splayerItemUpdate.playerItem.posType);
                itemData = new ItemData(splayerItemUpdate.playerItem);
                if (itemChaneNum[itemData.itemCode + "-" + splayerItemUpdate.updateType])
                {
                    itemChaneNum[itemData.itemCode + "-" + splayerItemUpdate.updateType].updateAmount = itemChaneNum[itemData.itemCode + "-" + splayerItemUpdate.updateType].updateAmount + splayerItemUpdate.updateAmount;
                }
                else
                {
                    itemChaneNum[itemData.itemCode + "-" + splayerItemUpdate.updateType] = {updateAmount:splayerItemUpdate.updateAmount, itemData:itemData};
                }
                switch(splayerItemUpdate.updateType)
                {
                    case EUpdateType._EUpdateTypeAdd:
                    {
                        if (itemData.itemInfo.group == EGroup._EGroupTask && packPosTypeCatch is BackPackCache)
                        {
                            if ((packPosTypeCatch as BackPackCache).moveInTaskBag(itemData))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("移入任务物品成功");
                            }
                        }
                        else if (itemData.itemInfo.group == EGroup._EGroupDenfenseCopy)
                        {
                            if (packPosTypeCatch.moveBagIn(itemData))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("移入掉落物品成功");
                            }
                            if (splayerItemUpdate.playerItem.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyBag || splayerItemUpdate.playerItem.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyRole)
                            {
                                this.processGetDefensePackItem(itemData, updataItems.code, splayerItemUpdate.playerItem.posType);
                            }
                        }
                        else if (itemData.itemInfo.group == EGroup._EGroupDrop)
                        {
                            if ((packPosTypeCatch as BackPackCache).moveInDropBag(itemData))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("移入掉落物品成功");
                            }
                        }
                        else if (packPosTypeCatch.moveBagIn(itemData))
                        {
                            aryPackItems.push({itemData:itemData});
                            this.addSpecialPetSkill(itemData);
                        }
                        NetDispatcher.dispatchCmd(ServerCommand.BackPackItemAdd, itemData);
                        break;
                    }
                    case EUpdateType._EUpdateTypeDel:
                    {
                        if (itemData.itemInfo.group == EGroup._EGroupTask && packPosTypeCatch is BackPackCache)
                        {
                            if ((packPosTypeCatch as BackPackCache).moveOutTaskBag(itemData.uid, splayerItemUpdate.playerItem))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("删除任务物品成功");
                            }
                        }
                        else if (itemData.itemInfo.group == EGroup._EGroupDenfenseCopy)
                        {
                            if (packPosTypeCatch.moveBagOut(splayerItemUpdate.playerItem.uid))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("删除掉落物品成功");
                            }
                        }
                        else if (itemData.itemInfo.group == EGroup._EGroupDrop)
                        {
                            if ((packPosTypeCatch as BackPackCache).moveOutDropBag(itemData.uid, splayerItemUpdate.playerItem))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("删除掉落物品成功");
                            }
                        }
                        else if (packPosTypeCatch.moveBagOut(splayerItemUpdate.playerItem.uid))
                        {
                            aryPackItems.push({itemData:itemData});
                        }
                        NetDispatcher.dispatchCmd(ServerCommand.BackPackItemDel, itemData);
                        break;
                    }
                    case EUpdateType._EUpdateTypeUpdate:
                    {
                        if (itemData.itemInfo.group == EGroup._EGroupTask && packPosTypeCatch is BackPackCache)
                        {
                            if ((packPosTypeCatch as BackPackCache).updateTastSPlayerItem(splayerItemUpdate.playerItem))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("更新任务物品成功");
                            }
                        }
                        else if (itemData.itemInfo.group == EGroup._EGroupDenfenseCopy)
                        {
                            if (packPosTypeCatch.updateSPlayerItem(splayerItemUpdate.playerItem))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("更新掉落物品成功");
                            }
                            if (splayerItemUpdate.updateAmount > 0 && splayerItemUpdate.playerItem.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyBag)
                            {
                                this.processGetDefensePackItem(itemData, updataItems.code);
                            }
                        }
                        else if (itemData.itemInfo.group == EGroup._EGroupDrop)
                        {
                            if ((packPosTypeCatch as BackPackCache).updateDropSPlayerItem(splayerItemUpdate.playerItem))
                            {
                                aryPackItems.push({itemData:itemData});
                                Log.debug("更新掉落物品成功");
                            }
                        }
                        else if (packPosTypeCatch.updateSPlayerItem(splayerItemUpdate.playerItem))
                        {
                            aryPackItems.push({itemData:itemData});
                            Log.debug("更新背包物品成功");
                        }
                        if (updataItems.code == UpdateCode.EUpdateCodeBagMove)
                        {
                            fromPackCache = Cache.instance.pack.getPackChacheByPosType(splayerItemUpdate.fromPosType);
                            aryFromPackItems = this.getArrayItems(splayerItemUpdate.fromPosType);
                            if (fromPackCache.moveBagOut(splayerItemUpdate.playerItem.uid))
                            {
                                aryFromPackItems.push({itemData:itemData});
                            }
                            if (packPosTypeCatch.moveBagIn(itemData))
                            {
                                aryPackItems.push({itemData:itemData});
                            }
                        }
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            var _loc_3:int = 0;
            var _loc_4:* = itemChaneNum;
            while (_loc_4 in _loc_3)
            {
                
                item = _loc_4[_loc_3];
                rmItemData = item.itemData;
                if (item.updateAmount > 0)
                {
                    if (updataItems.code == UpdateCode.EUpdateCodeBagMove)
                    {
                        if (rmItemData.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeBag && ItemsUtil.isGetUse(rmItemData))
                        {
                            guideData = new GuideUseItemData();
                            guideData.itemData = rmItemData;
                            guideData.isGuideToUseItem = true;
                            guideData.isCountdown = false;
                            Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideShowBackPackItem, guideData));
                        }
                    }
                    else
                    {
                        if (updataItems.code != UpdateCode.EUpdateCodeGetTodayOnlineReward)
                        {
                            MsgManager.addTipText(Language.getStringByParam(20516, item.updateAmount, ItemsUtil.getItemName(item.itemData)), MsgHistoryType.GetMsg);
                        }
                        else
                        {
                            tips = Language.getStringByParam(20516, item.updateAmount, ItemsUtil.getItemName(item.itemData));
                            Dispatcher.dispatchEvent(new DataEvent(EventName.LineOnCellGetGoodTip, tips));
                        }
                        if (rmItemData.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeBag && ItemsUtil.isGetUse(rmItemData))
                        {
                            GameProxy.packProxy.useItem(rmItemData.serverData.uid, rmItemData.itemInfo.code, rmItemData.serverData.itemAmount);
                        }
                        else
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.BackPackItemsAdd, [rmItemData, updataItems.code, item.updateAmount]);
                        }
                    }
                    continue;
                }
                if (item.updateAmount < 0)
                {
                    MsgManager.addTipText(Language.getStringByParam(20517, Math.abs(item.updateAmount), ItemsUtil.getItemName(item.itemData)), MsgHistoryType.LostMsg);
                    NetDispatcher.dispatchCmd(ServerCommand.BackPackItemsReduction, [rmItemData, updataItems.code]);
                }
            }
            aryChangeIndexBag = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeBag];
            if (aryChangeIndexBag && aryChangeIndexBag.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.BackPackItemsChange, null);
                NetDispatcher.dispatchCmd(ServerCommand.UpdateCapacity, null);
                if (updataItems.code == UpdateCode.EUpdateCodeBagTidy)
                {
                    (packPosTypeCatch as BackPackCache).sortItems();
                }
                else if (updataItems.code == UpdateCode.EUPdateCodeCollectFashion)
                {
                    obj;
                    obj["itemData"] = aryChangeIndexBag[0].itemData;
                    obj["isChange"] = true;
                    NetDispatcher.dispatchCmd(ServerCommand.WardrobeInfoUpdate, obj);
                }
                else if (updataItems.code == UpdateCode.EUpdateCodeBossDrop)
                {
                    NetDispatcher.dispatchCmd(ServerCommand.HangUpGetItem, aryChangeIndexBag[0].itemData);
                    NetDispatcher.dispatchCmd(ServerCommand.TreasureHuntGetItem, aryChangeIndexBag[0].itemData);
                }
                else if (updataItems.code == UpdateCode.EUpdateCodeBossDropDirToBagDir)
                {
                    this.guildGardenFlyItem(aryChangeIndexBag);
                }
                else if (updataItems.code == UpdateCode.EUpdateCodeLastHitBossDrop)
                {
                    NetDispatcher.dispatchCmd(ServerCommand.WorldBossLastHitDrop, aryChangeIndexBag);
                }
            }
            var aryChangeIndexRole:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeRole];
            if (aryChangeIndexRole && aryChangeIndexRole.length > 0)
            {
                equipType = (aryChangeIndexRole[0].itemData as ItemData).itemInfo.type;
                if (equipType < EEquip._EEquipFashion)
                {
                    NetDispatcher.dispatchCmd(ServerCommand.updateEquipMent, equipType);
                    updateInfo;
                    updateInfo.updateType = updataItems.code;
                    updateInfo.itemType = equipType;
                    updateInfo.posType = (aryChangeIndexRole[0].itemData as ItemData).posType;
                    NetDispatcher.dispatchCmd(ServerCommand.UpdateForgingEquip, updateInfo);
                }
            }
            var aryChangeIndexMount:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeMount];
            if (aryChangeIndexMount && aryChangeIndexMount.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.UpdateMountEquip, null);
            }
            var aryChangeIndexRun:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosRune];
            if (aryChangeIndexRun && aryChangeIndexRun.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.BackPackRunePackUpdate, null);
            }
            var aryChangeIndexPet:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypePet];
            if (aryChangeIndexPet && aryChangeIndexPet.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.UpdatePetEquip, null);
            }
            var aryChangeIndexHalidom:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosGodHead];
            if (aryChangeIndexHalidom && aryChangeIndexHalidom.length > 0)
            {
                hType = (aryChangeIndexHalidom[0].itemData as ItemData).itemInfo.type;
                NetDispatcher.dispatchCmd(ServerCommand.RoleHalidomUpdate, hType);
            }
            var aryChangeIndexWarehouse:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeWarehouse];
            if (aryChangeIndexWarehouse && aryChangeIndexWarehouse.length > 0)
            {
                if (updataItems.code == UpdateCode.EUpdateCodeBagTidy)
                {
                    (packPosTypeCatch as WarehouseCache).sortItems();
                }
                NetDispatcher.dispatchCmd(ServerCommand.UpdateWarehouse, null);
            }
            var aryChangeIndexDefenseCopyBag:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyBag];
            if (aryChangeIndexDefenseCopyBag && aryChangeIndexDefenseCopyBag.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.DefenseCopyBagUpdate, null);
                this.processGetDefensePackItem(itemData, updataItems.code, -1);
            }
            var aryChangeIndexDefenseRoleEquip:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyRole];
            if (aryChangeIndexDefenseRoleEquip && aryChangeIndexDefenseRoleEquip.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.DefenseCopyRoleEquipUpdate, null);
                if (updataItems.code == UpdateCode.EUpdateCodeBagMove || updataItems.code == UpdateCode.EUpdateCodeDefenseCopy)
                {
                    NetDispatcher.dispatchCmd(ServerCommand.DefenseCopyRoleEquipAdd, itemData);
                }
            }
            var aryChangeIndexDecomposeBag:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeDecompose];
            if (aryChangeIndexDecomposeBag && aryChangeIndexDecomposeBag.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.ForgingDecomposeBagUpdate, null);
            }
            var aryChangeIndexLotteryDrawBag:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosLottery];
            if (aryChangeIndexLotteryDrawBag && aryChangeIndexLotteryDrawBag.length > 0)
            {
                if (updataItems.code == UpdateCode.EUpdateCodeBagTidy)
                {
                    (packPosTypeCatch as lotteryDrawPackageCache).sortItems();
                }
                NetDispatcher.dispatchCmd(ServerCommand.LotteryDrawBagInfoUpdate, null);
            }
            var i:int;
            while (i <= 9)
            {
                
                aryChangeIndexFashion = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTypeFashion + i];
                if (aryChangeIndexFashion && aryChangeIndexFashion.length > 0)
                {
                    switch(i)
                    {
                        case EAdvanceComponentType._EAdvanceComponentTypeClothes:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionClothEquipUpdate, i);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeWing:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionWingEquipUpdate, i);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeWeapon:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionWeaponEquipUpdate, i);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeFightSoul:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionFightSoulEquipUpdate, null);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeFightMethod:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionFightMethodEquipUpdate, null);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeDragonSoul:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionDragonSoulEquipUpdate, null);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeHolyBook:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionHolyBookEquipUpdate, null);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeWarShield:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionWarShieldEquipUpdate, null);
                            break;
                        }
                        case EAdvanceComponentType._EAdvanceComponentTypeMonsterSoul:
                        {
                            NetDispatcher.dispatchCmd(ServerCommand.FashionMonsterSoulEquipUpdate, null);
                            break;
                        }
                        default:
                        {
                            break;
                        }
                    }
                }
                i = (i + 1);
            }
            var aryChangeIndexTowerScan:* = objChangeIndexs[EPlayerItemPosType._EPlayerItemPosTowerScan];
            if (aryChangeIndexTowerScan && aryChangeIndexTowerScan.length > 0)
            {
                NetDispatcher.dispatchCmd(ServerCommand.TowerSweepPackUpdate, null);
            }
            return;
        }// end function

        private function processGetDefensePackItem(param1:ItemData, param2:int, param3:int = 0) : void
        {
            var _loc_5:Point = null;
            var _loc_6:Point = null;
            var _loc_4:Array = [];
            if (param3 != -1 && GameController.copy.defenseCopy.navBar != null && param2 == UpdateCode.EUpdateCodeBossDrop)
            {
                _loc_5 = new Point(Global.stage.stageWidth / 2, Global.stage.stageHeight / 2);
                _loc_6 = GameController.copy.defenseCopy.navBar.getBtnBagGlobalPoint();
                _loc_4[0] = param1;
                _loc_4[1] = _loc_5;
                _loc_4[2] = _loc_6;
                Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToAnyPoint, _loc_4));
            }
            else if (param2 == UpdateCode.EUpdateCodeDefenseCopy && (param3 == -1 || param3 == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyRole))
            {
                if (param3 == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyRole)
                {
                    _loc_6 = GameController.copy.defenseCopy.navBar.getEquipItemPointByUid(param1.uid);
                }
                else if (DefenseCopyBag.hasInstance && DefenseCopyBag.instance.isHide == false)
                {
                    _loc_6 = DefenseCopyBag.instance.getItemPointByUid(param1.uid);
                    if (_loc_6 == null)
                    {
                        return;
                    }
                }
                else
                {
                    _loc_6 = GameController.copy.defenseCopy.navBar.getBtnBagGlobalPoint();
                }
                _loc_5 = DefenseCopyShopEquipView.instance.baseItemPoint;
                _loc_4[0] = param1;
                _loc_4[1] = _loc_5;
                _loc_4[2] = _loc_6;
                Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToAnyPoint, _loc_4));
            }
            return;
        }// end function

        private function addSpecialPetSkill(param1:ItemData) : void
        {
            if (ItemsUtil.isPetSkill1(param1) && !ClientSetting.local.getIsDone(IsDoneType.HasPetSkill1))
            {
                ClientSetting.local.setIsDone(true, IsDoneType.HasPetSkill1);
                ClientSetting.save();
            }
            else if (ItemsUtil.isPetSkill2(param1) && !ClientSetting.local.getIsDone(IsDoneType.HasPetSkill2))
            {
                ClientSetting.local.setIsDone(true, IsDoneType.HasPetSkill2);
                ClientSetting.save();
            }
            else if (ItemsUtil.isPetSkill3(param1) && !ClientSetting.local.getIsDone(IsDoneType.HasPetSkill3))
            {
                ClientSetting.local.setIsDone(true, IsDoneType.HasPetSkill3);
                ClientSetting.save();
            }
            else if (ItemsUtil.isPetSkill4(param1) && !ClientSetting.local.getIsDone(IsDoneType.HasPetSkill4))
            {
                ClientSetting.local.setIsDone(true, IsDoneType.HasPetSkill4);
                ClientSetting.save();
            }
            return;
        }// end function

        private function guildGardenFlyItem(param1:Array) : void
        {
            this._gardenFlyArr = this._gardenFlyArr.concat(param1);
            if (this._gardenFlyArr == null || this._gardenFlyArr.length == 0)
            {
                return;
            }
            if (!this._frameT)
            {
                this._frameT = new FrameTimer(20);
                this._frameT.addListener(TimerType.ENTERFRAME, this.onTimerFrameHandler);
            }
            if (!this._frameT.running)
            {
                this._frameT.start();
            }
            return;
        }// end function

        private function onTimerFrameHandler(param1:FrameTimer) : void
        {
            if (this._gardenFlyArr == null || this._gardenFlyArr.length == 0)
            {
                this._frameT.stop();
                this._frameT.dispose();
                this._frameT = null;
                return;
            }
            var _loc_2:* = this._gardenFlyArr.pop().itemData;
            Dispatcher.dispatchEvent(new DataEvent(EventName.FlyItemToPack, _loc_2));
            return;
        }// end function

    }
}
